<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import= "com.roadmap.struts.action.ActionConst"%>
<%@ page import="com.roadmap.common.RdcResourceBundle" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String user_ssn_id = (String)request.getSession().getAttribute(ActionConst.Aspirant_Id_In_Session);
String user_req_id = (String)request.getParameter(ActionConst.Aspirant_Id_In_Request);
if(user_req_id == null){
	user_req_id = user_ssn_id;
}
String article_req_id = (String)request.getParameter(ActionConst.Item_Id_In_Request);
if(article_req_id == null){
	article_req_id = "";
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/> 
<link href="res/css/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/life.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/widget.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/BlogHandler.js"></script>
<script type="text/javascript" src="dwr/interface/RdcAjax.js"></script>
<script type="text/javascript" src="res/js/func.js"></script>
<script type="text/javascript" src="res/js/widget.js"></script>
<script type="text/javascript" src="res/js/component.js"></script>

<title><logic:present name="article"><bean:write name="article" property="title"/>| </logic:present>总结感悟 | <%=RdcResourceBundle.COMPANY_NAME %></title>
</head>

<body id="container">
	<!-- define header including menus and logo -->
	<jsp:include page="/admin/myheader.jsp"></jsp:include>
	
	<!-- main body -->
	<div class="mainbody"><div class="manager">
	<jsp:include page="/admin/life/nav_btns.jsp"/>
			
	<!-- middle part: content -->
	<div class="middle-part">
		<div class="section">
			<div class="title-sp">返回<a href="aspirant.do?uid=<%=user_req_id %>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>"/></a>的<a href="blog.do?act=all&uid=<%=user_req_id %>">总结感悟列表</a></div>
			
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<div class="float-right"><a class="linkBtn" href="javascript:goToCreate()">发表新总结感悟</a></div>
			</logic:present>
		</div>
		
		<logic:notPresent name="article">
		<div class="prompt-message" id="artDoesntExist" style="display:none;text-align:center;">您要查看的总结感悟不存在</div>
		
		<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
		<!-- Following part is used when user is going to create a new article -->
		<div id="viewArticleDiv" class="article" style="display:none;">
			<div class="title">
				<h3><span id="viewTitle"></span></h3>
				<div>
					<span class="tstamp">
						<a href="aspirant.do?uid=<%=user_req_id %>/>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>" /></a>发表于<span id="viewCreated"></span>,更新于<span id="viewUpdated"></span>
					</span>
					<span class="float-right">
						<input type="image" src="res/images/logo/edit_10.png" title="编辑" onclick="goToEdit();"/>&nbsp;
						<input type="image" src="res/images/logo/delete_10.png" title="删除" onclick="deltArticle();"/>
					</span>
				</div>
			</div>
			<div class="content">
				<span id="viewContent"></span>
			</div>
		</div>	
		</logic:present>
		</logic:notPresent>
		
		<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
		<div id="editArticleDiv" class="article" style="display:none;">
			<div class="title"><label>标题：</label><input id="title" type="text" class="input-text" size="70" title="标题(1~50)"/></div>
			<div class="content"><textarea id="content" class="input-text" style="width:100%;height:100px;" title="内容(1~280)"></textarea></div>
				<!-- <script type="text/javascript">WYSIWYG.attach('content', small);</script>  -->
			<div class="foot">
				<input id="saveBtn" type="button" class="confirmBtn" value="发布总结感悟" onclick="saveArticle();"/> <!-- WYSIWYG.updateTextArea('content'); -->
				<input id="cancelBtn" type="button" class="cancelBtn" value="取消" onclick="cancelEdit();"/>
			</div>
		</div>
		</logic:present>
		
		<logic:present name="article">
		<div id="viewArticleDiv" class="article">
			<div class="title">
				<h3><span id="viewTitle"><bean:write name="article" property="title"/></span></h3>
				<div>
					<span class="tstamp">
						<a href="aspirant.do?uid=<bean:write name='article' property='aspirantId'/>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>"/></a>
						发表于<span id="viewCreated"><bean:write format="yyyy-MM-dd HH:mm:ss" name="article" property="createdTime"/></span>,
						更新于<span id="viewUpdated"><bean:write format="yyyy-MM-dd HH:mm:ss" name="article" property="updatedTime"/></span>
					</span>
					
					<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
					<span class="float-right">
						<input type="image" src="res/images/logo/edit_10.png" title="编辑" onclick="goToEdit();"/>&nbsp;
						<input id="deltBtn" type="image" src="res/images/logo/delete_10.png" title="删除" onclick="deltArticle();"/>
					</span>
					</logic:present>
				</div>
			</div>
			<div id="viewContent" class="content">
				<bean:write name="article" property="content"/>
			</div>
		</div>
		
		<!-- show comments -->
		<jsp:include page="/pagelet/commentPagelet.jsp"/>
		</logic:present>
		
	</div>
		
	<!-- right part: -->
	<div class="right-part">
	</div>
	
	</div>
	
	<div class="clear-div"></div>
	</div>
	
	<jsp:include page="/res/jsp/footer.jsp"></jsp:include>

<script type="text/javascript">

var _isEditing = false; // create or edit an article
var commentUtil = null;

window.onload = function(){
	var _action = "<%=request.getParameter(ActionConst.Method_Symbol)%>";
	if(_action != "create" && _action != "view"){
		PromptDialog.show("您输入的网址有误");
	}
	else if(_action == "create"){
		goToCreate();
	}
	else {
		commentUtil = new CommentUtil("Article", "<%=article_req_id%>", "<%=user_ssn_id%>", "<%=user_req_id%>");
		RDC.gui.display("artDoesntExist");
	}
}
</script>

<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
<script type="text/javascript">
function goToView(){
	RDC.gui.hide("editArticleDiv");
	RDC.gui.display("viewArticleDiv");
}

function goToCreate(){
	RDC.gui.hide("artDoesntExist");
	RDC.gui.hide("viewArticleDiv");
	RDC.gui.display("editArticleDiv");
	$("title").focus();
	$("title").value = "";
	//$("content").value = "";
	_isEditing = false;
}

function goToEdit(){
	RDC.gui.hide("viewArticleDiv");
	RDC.gui.display("editArticleDiv");
	$("title").focus();
	$("title").value = $("viewTitle").innerHTML;
	$("content").value = $("viewContent").innerHTML;
	//WYSIWYG.insertHTML($("viewContent").innerHTML, "content");
	_isEditing = true;
}

function cancelEdit(){
	if(_isEditing){
		RDC.gui.hide("editArticleDiv");
		RDC.gui.display("viewArticleDiv");
	}
	else {
		RDC.gui.hide("editArticleDiv");
		window.location.href = "blog.do?act=all";
	}
}

function checkInputs(){
	if(!RDC.util.verifyText($("title").value, 1, 50)){
		PromptDialog.show("文章标题的字数在1~50之间");
		$("title").focus();
		return false;
	}
	if(!RDC.util.verifyText($("content").value, 1, 280)){
		PromptDialog.show("文章内容的字数在1~280之间");
		$("content").focus();
		return false;
	}
	
	return true;
}

function saveArticle(){
	if(!checkInputs()){
		return;
	}
	
	var art = {id:null};
	if(_isEditing){
		art.id = "<%=article_req_id%>";
		art.createdTime = RDC.util.stringToDate($("viewCreated").innerHTML, "yyyy-MM-dd HH:mm:ss");
	}
	else {
		art.id = null;
	}
	art.title = $("title").value;
	art.content = $("content").value;
	art.aspirantId = "<%=user_ssn_id%>";
	
	RDC.gui.disable("saveBtn");
	BlogHandler.saveArticle(art.aspirantId, art, function(result){
		if(result == null){
			PromptDialog.show("保存总结感悟失败，请稍候再试");
		}
		else {
			window.location.href = "blog.do?act=view&uid=" + result.aspirantId + "&id=" + result.id;
		}
		
		RDC.gui.enable("saveBtn");
	});
}

function deltArticle(){
	var uid = "<%=user_ssn_id%>";
	var id = "<%=article_req_id%>";
	RDC.gui.disable("deltBtn");
	BlogHandler.deleteArticle(uid, id, function(result){
		if(result){
			window.location.href = "blog.do?act=all&uid=" + uid;
		}
		else {
			PromptDialog.show("删除总结感悟失败");
			RDC.gui.enable("deltBtn");
		}
	});
}

</script>
</logic:present>

</body>
</html>
